/**
 * Copyright (C), 2015-2018, 上海金念有限公司
 * FileName: LeaveActivitiTest
 * Author:   baokang
 * Date:     2018/6/6 9:51
 * Description: 第一个activiti例子
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package activiti;

import org.activiti.engine.*;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈第一个activiti例子〉
 *
 * @author baokang
 * @create 2018/6/6
 * @since 1.0.0
 */
public class LeaveActivitiTest {

    /**
     * 会默认按照Resources目录下的activiti.cfg.xml创建流程引擎
     */
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();//同下
    //ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
    @Test
    public void test() {
        //以下两种方式选择一种创建引擎方式：1.配置写在程序里 2.读对应的配置文件
        //1
//        testCreateProcessEngine();
        //2
//        testCreateProcessEngineByCfgXml();


        deployProcess();//发布流程
        startProcess();//启动流程
        queryTask();//查看任务
        handleTask();//办理任务
    }

    /**
     * 测试activiti环境
     */
    @Test
    public void testCreateProcessEngine() {
        ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        cfg.setJdbcDriver("com.mysql.jdbc.Driver");
        cfg.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/activiti");
        cfg.setJdbcUsername("root");
        cfg.setJdbcPassword("123456");
        //配置建表策略
        cfg.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);//如果表不存在，就创建表，如果表存在，就直接操作
//        ProcessEngine engine = cfg.buildProcessEngine();
    }

    /**
     * 根据配置文件activiti.cfg.xml创建ProcessEngine
     */
    @Test
    public void testCreateProcessEngineByCfgXml() {
        ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        //ProcessEngine engine = cfg.buildProcessEngine();
    }

    /**
     * 发布流程（classpath方式）
     * RepositoryService
     */
    @Test
    public void deployProcess() {
        RepositoryService repositoryService = processEngine.getRepositoryService();//创建引擎
        DeploymentBuilder builder = repositoryService.createDeployment();//创建DeploymentBuilder实例
        builder.addClasspathResource("diagrams/bk.xml");//添加资源
        builder.deploy();//执行部署（写入到数据库中）
    }

    /**
     * 启动流程
     * RuntimeService
     */
    @Test
    public void startProcess() {
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //可根据id、key、message启动流程
        runtimeService.startProcessInstanceByKey("myProcess_1");
    }

    /**
     * 查看任务
     * TaskService
     */
    @Test
    public void queryTask() {
        TaskService taskService = processEngine.getTaskService();
        //根据assignee(代理人)查询任务
        String assignee = "manager";
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();

//        int size = tasks.size();
//        for (int i = 0; i < size; i++) {
//            Task task = tasks.get(i);
//
//        }


        for (Task task : tasks) {
            System.out.println("taskId:" + task.getId() +
                    ",taskName:" + task.getName() +
                    ",assignee:" + task.getAssignee() +
                    ",createTime:" + task.getCreateTime());
        }
    }

    /**
     * 办理任务
     */
    @Test
    public void handleTask() {
        TaskService taskService = processEngine.getTaskService();
        //根据上一步生成的taskId执行任务
        String taskId = "2502";
        taskService.complete(taskId);
    }

}